package com.thinkfit.jy.activity;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.common.lib.activity.BaseActivity;
import com.common.lib.constant.Constants;
import com.common.lib.mvp.contract.EmptyContract;
import com.common.lib.mvp.presenter.EmptyPresenter;
import com.thinkfit.jy.R;
import com.thinkfit.jy.serialport.SerialPortManager;
import com.thinkfit.jy.utils.BluetoothOrder;
import com.thinkfit.jy.utils.DeviceTypeUtils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * 倒计时界面
 */
public class CountDownActivity extends BaseActivity<EmptyContract.Presenter> implements EmptyContract.View {

    private Disposable mDisposableCountDown;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_count_down;
    }

    @Override
    protected void onCreated(@Nullable Bundle savedInstanceState) {
        WindowManager.LayoutParams params = getWindow().getAttributes();
        getWindow().setAttributes(params);
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        setTextTypeFace(R.id.tvCount);
        int time = getIntent().getExtras().getInt(Constants.BUNDLE_EXTRA);
        setText(R.id.tvCount, String.valueOf(time));
        Observable.interval(1,  TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mDisposableCountDown = d;
                    }

                    @Override
                    public void onNext(Long aLong) {
                        long l = aLong + 1;
                        setText(R.id.tvCount, String.valueOf(time - l));
                        if (l == time) {
                            closeDisposableCountDown();
                            if(DeviceTypeUtils.connectDeviceType() == 0){
                                if(DeviceTypeUtils.getInstance().isTreadmill()){
                                    BluetoothOrder.getInstance().startTreadmill();
                                }else {
                                    System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa    4444");
                                    BluetoothOrder.getInstance().startPlay();
                                }
                            }else {
                                SerialPortManager.getInstance().startRun();
                            }
                            finish();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        closeDisposableCountDown();
                    }

                    @Override
                    public void onComplete() {
                        closeDisposableCountDown();
                    }
                });
    }


    @NonNull
    @Override
    protected EmptyContract.Presenter onCreatePresenter() {
        return new EmptyPresenter(this);
    }


    @Override
    public void onClick(View v) {
    }


    @Override
    protected void onStop() {
        super.onStop();
        System.out.println("ddddddddddddddddddddddd   CountDownActivity ");
        DeviceTypeUtils.showBackButton(getApplicationContext());
    }

    public void onDestroy() {
        super.onDestroy();
        closeDisposableCountDown();
    }

    public void closeDisposableCountDown(){
        if (mDisposableCountDown != null) {
            mDisposableCountDown.dispose();
            mDisposableCountDown = null;
        }
    }

}